﻿using PSDManager.SystemSettings.RouteSetting;
using PSDManager.SystemSettings.VersionManager;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PSDManager.SystemSettings
{
    public partial class FrmSettings : Form
    {
        private Label currSettingItem;

        private Dictionary<string, ISettingPanel> settingPanels = new Dictionary<string, ISettingPanel>();

        public ManagerHelper ManagerHelper { get; set; }
        public FrmSettings()
        {
            InitializeComponent();

            this.Load += FrmSettings_Load;
        }

        private void FrmSettings_Load(object sender, EventArgs e)
        {
            NetworkSettingPanel networkSettingPanel = new NetworkSettingPanel();
            networkSettingPanel.ManagerHelper = ManagerHelper;
            palSettings.Controls.Add(networkSettingPanel);
            networkSettingPanel.Dock = DockStyle.Fill;
            settingPanels.Add(miNetworkSettings.Name, networkSettingPanel);

            ChannelSettingPanel channelSettingPanel = new ChannelSettingPanel();
            channelSettingPanel.ManagerHelper = ManagerHelper;
            palSettings.Controls.Add(channelSettingPanel);
            channelSettingPanel.Dock = DockStyle.Fill;
            settingPanels.Add(miChannelSettings.Name, channelSettingPanel);

            DeviceSettingPanel deviceSettingPanel = new DeviceSettingPanel();
            deviceSettingPanel.ManagerHelper = ManagerHelper;
            palSettings.Controls.Add(deviceSettingPanel);
            deviceSettingPanel.Dock = DockStyle.Fill;
            settingPanels.Add(miDeviceSettings.Name, deviceSettingPanel);

            BurnSettingPanel burnSettingPanel = new BurnSettingPanel();
            palSettings.Controls.Add(burnSettingPanel);
            burnSettingPanel.Dock = DockStyle.Fill;
            settingPanels.Add(miBurnSettings.Name, burnSettingPanel);

            MsgSettingPanel msgSettingPanel = new MsgSettingPanel();
            palSettings.Controls.Add(msgSettingPanel);
            msgSettingPanel.Dock = DockStyle.Fill;
            settingPanels.Add(miMsgSettings.Name, msgSettingPanel);

            RouteChannelSettingPanel routeSettingPanel = new RouteChannelSettingPanel();
            palSettings.Controls.Add(routeSettingPanel);
            routeSettingPanel.Dock = DockStyle.Fill;
            settingPanels.Add(miRouteSettings.Name, routeSettingPanel);

            VersionManagerPanel versionManagerPanel = new VersionManagerPanel();
            versionManagerPanel.ManagerHelper = this.ManagerHelper;
            palSettings.Controls.Add(versionManagerPanel);
            versionManagerPanel.Dock = DockStyle.Fill;
            settingPanels.Add(miVersionManager.Name, versionManagerPanel);

            LoadSettings();

            SetCurrentSetting(miNetworkSettings);
        }

        private void SetCurrentSetting(Label miSetting)
        {
            if (currSettingItem != null)
            {
                currSettingItem.BackColor = Color.Transparent;
            }

            currSettingItem = miSetting;

            currSettingItem.BackColor = Color.LightSteelBlue;

            (settingPanels[currSettingItem.Name] as Control).BringToFront();
        }

        private void MiSettings_MouseEnter(object sender, EventArgs e)
        {
            if (sender != currSettingItem)
            {
                (sender as Label).BackColor = Color.PowderBlue;
            }
        }

        private void MiSettings_MouseLeave(object sender, EventArgs e)
        {
            if (sender != currSettingItem)
            {
                (sender as Label).BackColor = Color.Transparent;
            }
        }

        private void MiSettings_Click(object sender, EventArgs e)
        {
            SetCurrentSetting((sender as Label));
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            ApplySettings();
            this.Close();
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            ApplySettings();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void LoadSettings()
        {
            foreach (KeyValuePair<string, ISettingPanel> item in settingPanels)
            {
                item.Value.LoadSettings();
            }
        }

        private void ApplySettings()
        {
            foreach (KeyValuePair<string, ISettingPanel> item in settingPanels)
            {
                item.Value.ApplySettings();
            }

            AppSettings.Enabled = AppSettings.Setting.Save();
        }

        private void SaveSettings()
        {
            foreach (KeyValuePair<string, ISettingPanel> item in settingPanels)
            {
                item.Value.SaveSettings();
            }

            AppSettings.Enabled = AppSettings.Setting.Save();
        }
    }
}
